/**
 * 私有路由
 */
import React from 'react';
import {Route, withRouter} from 'react-router-dom';
import { message } from "antd";
import PropTypes from 'prop-types';
import storage from "../common/util/storage";
import { isPermission } from "../common/util/util"

//私有路由，只有管理员才能访问
class PrivateRoute extends React.Component{
    componentWillMount(){
        let roles = storage.getSession("userRoles");
        let auth = this.props.auth;
        let  isAuthenticated =  isPermission(roles, auth);
        this.setState({isAuthenticated:isAuthenticated})
        if(!isAuthenticated){
            message.destroy();
            message.info("您无访问权限，跳转回首页。")
            const {history} = this.props;
            setTimeout(() => {
                history.replace("/layout/taskhall");
            }, 1000)
        }
    }
    render(){
        let { component: Component,path="/",exact=false,strict=false} = this.props;
        return this.state.isAuthenticated ? <Route  path={path} exact={exact}  strict={strict}  render={(props)=>( <Component {...props} /> )} />:""
    }
}
PrivateRoute.propTypes  ={
        path:PropTypes.string.isRequired,
        exact:PropTypes.bool,
        strict:PropTypes.bool,
        component:PropTypes.func.isRequired,
        auth:PropTypes.string.isRequired
}
export default withRouter(PrivateRoute);